/**
*JungleGo
*Owners: Kidon Son, Kevin Choi
*Date: Sept 2011
*hm......
*/


#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#endif

//CLASS INCLUDES
#include <time.h>
#include "glHelper.hpp"
#include "types.hpp"
#include "world.hpp"
using namespace std;

const uint CR_NUM = 200;
const uint XSize = 1024;
const uint YSize = 550;

//Global
float angle=0.0f;
uint gFramesPerSecond=60;
clock_t gClock;
int doPause = -1;
World world;

void reshape (int width, int height) {
    glViewport(0, 0, (GLsizei)width, (GLsizei)height); // Set our viewport to the size of our window
    glMatrixMode(GL_PROJECTION); // Switch to the projection matrix so that we can manipulate how our scene is viewed

    // Reset the projection matrix to the identity matrix
    // so that we don't paintget any artifacts (cleaning up)
    glLoadIdentity();

    // Disable Z check
    glDisable(GL_DEPTH_TEST);

    // Set the Field of view angle (in degrees),
    // the aspect ratio of our window, and the new and far planes
    //gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0);
    glOrtho(0, width, height, 0, 0, 1);

    // Switch back to the model view matrix, so that we can start drawing shapes correctly
    glMatrixMode(GL_MODELVIEW);
}

void keyboard_check(unsigned char key, int x, int y) {
    switch(key) {
    case 'q':
    case 'Q':
        //quit application
        cout<<"Quitting JungleGo"<<endl;
        exit (0);   //todo: more graceful exit
        break;
    case 's':
    case 'S':
        //pause opengl
        if(doPause!=1)
            cout<<"JungleGo Paused"<<endl;
        else
            cout<<"JungleGo Unpaused"<<endl;
        doPause*=-1;
        break;
    case 'p':
    	//fuck
    	world.AttractCreatures(x, y);
    	break;
    case 'l':
    	world.SlowDownAll();
    	break;
    default:
    	break;
    }
    glutPostRedisplay();
}

void mouse_check(int button, int state, int x, int y) {
    switch(button) {
    case GLUT_LEFT_BUTTON:
        if(button==GLUT_UP) {

        } else if(button==GLUT_DOWN) {

        } else {

        }
        break;
    case GLUT_RIGHT_BUTTON:

        break;
    case GLUT_MIDDLE_BUTTON:

        break;
    }
    glutPostRedisplay();
}
void display() {
    world.paint();
}

void animate() {
#if defined(_WIN32) || defined(_WIN64)
    Sleep(1000/gFramesPerSecond);
#else
    usleep(1000000/gFramesPerSecond);
#endif
    if(doPause!=1) {
        world.update();
        glutPostRedisplay();
    }
}

void initCore() {
    world.init(CR_NUM, 10, XSize, YSize);
}

int main(int argc, char **argv) {
    gClock = clock();
    glutInit(&argc, argv); // Initialize GLUT
    glutInitDisplayMode (GLUT_DOUBLE); // Set up a basic display double buffer
    glutInitWindowSize (XSize, YSize); // Set the width and height of the window
    glutInitWindowPosition (0, 0); // Set the position of the window
    glutCreateWindow ("Welcome to Jungle Go"); // Set the title for the window

    ///Do stuff here
    initCore();

    glutDisplayFunc(display); // Tell GLUT to use the method "display" for rendering
    glutReshapeFunc(reshape); // Tell GLUT to use the method "reshape" for rendering

    glutIdleFunc(animate); // Tell GLUT to run this anytime he can

    glutKeyboardFunc(keyboard_check);   // Keyboard
    glutMouseFunc(mouse_check);         // Mouse

    glutMainLoop(); // Enter GLUT's main loop
    return 1;
}

